package com.chenjt.springai.six_functionCalling.repository;

import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 注释：可以参考 InMemoryChatMemoryRepository.class去做，
 *       官网对这个（InMemoryChatMemoryRepository）的介绍很少，
 *       上面写通过new的方式去创建
 *
 * @author chenjt
 * @date 2025/5/28 16:08
 */
@Component
public class InMemoryChatHistoryRepository implements ChatHistoryRepository {

    private final Map<String,List<String>> chatHistory = new HashMap<>();

    @Override
    public void save(String type, String chatId) {
//        if(!chatHistory.containsKey(type)){
//            chatHistory.put(type,new ArrayList<>());
//        }
//        List<String> chatIds = chatHistory.get(type);
        List<String> chatIds = chatHistory.computeIfAbsent(type, k -> new ArrayList<>());
        if(chatIds.contains(chatId)){
            return;
        }
        chatIds.add(chatId);
    }

    @Override
    public List<String> getChatIds(String type) {
//        List<String> chatIds = chatHistory.get(type);
//        return chatIds == null ? new ArrayList<>() : chatIds;
//        return chatHistory.getOrDefault(type, List.of());
        return chatHistory.getOrDefault(type, new ArrayList<>());
    }

}
